﻿/*=================================================================
* 版权信息：Copyright (C) 2005-2011 捷点科技 All Rights Reserved.
* 文件名称：CNVP_Order.cs
* 创建标识：曾乃强 15158756652 2011-04-14 04:04:15
* 修改标识：
* 功能描述：
===================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Config;

namespace CNVP.JShop.DAL
{
    /// <summary>
    /// 对*CNVP_Order*操作数据库(针对SQL Server)的实现
    /// </summary>
    public class CNVP_Order
    {
       private string Prefix = DBConfig.Prefix;
        #region DAL
        /// <summary>
        /// 添加
        /// </summary>
        public int Insert(Model.CNVP_Order obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into  " + Prefix + "Order ([OrderNum],[AddTime],[PostTime],[ProductsTotalAmount],[OrderTotalAmount],[DeliveryWay],[DeliveryPrice],[PayWay],[IsNeedBill],[MobilePhone],[Phone],[ZipCode],[GetTrueName], [Province], [City], [Area],[GetAddress],[BuyerID],[OrderState],[Text])");
            sql.Append("values(@OrderNum,@AddTime,@PostTime,@ProductsTotalAmount,@OrderTotalAmount,@DeliveryWay,@DeliveryPrice,@PayWay,@IsNeedBill,@MobilePhone,@Phone,@ZipCode,@GetTrueName, @Province, @City,@Area,@GetAddress,@BuyerID,@OrderState,@Text)");
            sql.Append(";SELECT @@IDENTITY");

            SqlParameter[] pra = {
                            new SqlParameter("@OrderNum",obj.OrderNum),
							new SqlParameter("@AddTime",obj.AddTime),
                            new SqlParameter("@PostTime",obj.PostTime),
							new SqlParameter("@ProductsTotalAmount",obj.ProductsTotalAmount),
							new SqlParameter("@OrderTotalAmount",obj.OrderTotalAmount),
							new SqlParameter("@DeliveryWay",obj.DeliveryWay),
							new SqlParameter("@DeliveryPrice",obj.DeliveryPrice),
							new SqlParameter("@PayWay",obj.PayWay),
							new SqlParameter("@IsNeedBill",obj.IsNeedBill),
							new SqlParameter("@MobilePhone",obj.MobilePhone),
							new SqlParameter("@Phone",obj.Phone),
                            new SqlParameter("@ZipCode",obj.ZipCode),
							new SqlParameter("@GetTrueName",obj.GetTrueName),
							new SqlParameter("@Province",obj.Province),
							new SqlParameter("@City",obj.City),
							new SqlParameter("@Area",obj.Area),
							new SqlParameter("@GetAddress",obj.GetAddress),
							new SqlParameter("@BuyerID",obj.BuyerID),
                            new SqlParameter("@OrderState",obj.OrderState),
                            new SqlParameter("@Text",obj.Text)
                                 };
            return Convert.ToInt32(DbHelper.ExecuteScalar(sql.ToString(), pra));
        }
        /// <summary>
        /// 修改
        /// </summary>
        public int Update(Model.CNVP_Order obj)
        {
             StringBuilder sql = new StringBuilder();
             sql.Append("update " + Prefix + "Order set [OrderNum] = @OrderNum,[ProductsTotalAmount] = @ProductsTotalAmount,[OrderTotalAmount] = @OrderTotalAmount,[DeliveryWay] = @DeliveryWay,[DeliveryPrice] = @DeliveryPrice,[PayWay] = @PayWay,[IsSupportValue] = @IsSupportValue,[SupportValuePrice] = @SupportValuePrice,[ProductWeight] = @ProductWeight,[PayFactorage] = @PayFactorage,[BillRises] = @BillRises,[IsNeedBill] = @IsNeedBill,[BilTaxes] = @BilTaxes,[PayCurrency] = @PayCurrency,[SalesPromotionAmount] = @SalesPromotionAmount,[DiscountOrder] = @DiscountOrder,[MobilePhone] = @MobilePhone,[Phone] = @Phone,[Email] = @Email,[PostTime] = @PostTime,[ZipCode] = @ZipCode,[GetTrueName] = @GetTrueName,[Province]=@Province,[City]=@City,[Area]=@Area,[GetAddress] = @GetAddress,[Text]=@Text where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@OrderNum",obj.OrderNum),
							new SqlParameter("@ProductsTotalAmount",obj.ProductsTotalAmount),
							new SqlParameter("@OrderTotalAmount",obj.OrderTotalAmount),
							new SqlParameter("@DeliveryWay",obj.DeliveryWay),
							new SqlParameter("@DeliveryPrice",obj.DeliveryPrice),
							new SqlParameter("@PayWay",obj.PayWay),
							new SqlParameter("@IsSupportValue",obj.IsSupportValue),
							new SqlParameter("@SupportValuePrice",obj.SupportValuePrice),
							new SqlParameter("@ProductWeight",obj.ProductWeight),
							new SqlParameter("@PayFactorage",obj.PayFactorage),
							new SqlParameter("@BillRises",obj.BillRises),
							new SqlParameter("@IsNeedBill",obj.IsNeedBill),
							new SqlParameter("@BilTaxes",obj.BilTaxes),
							new SqlParameter("@PayCurrency",obj.PayCurrency),
							new SqlParameter("@SalesPromotionAmount",obj.SalesPromotionAmount),
							new SqlParameter("@DiscountOrder",obj.DiscountOrder),
							new SqlParameter("@MobilePhone",obj.MobilePhone),
							new SqlParameter("@Phone",obj.Phone),
							new SqlParameter("@Email",obj.Email),
							new SqlParameter("@PostTime",obj.PostTime),
							new SqlParameter("@ZipCode",obj.ZipCode),
							new SqlParameter("@GetTrueName",obj.GetTrueName),
                            new SqlParameter("@Province",obj.Province),
							new SqlParameter("@City",obj.City),
							new SqlParameter("@Area",obj.Area),
							new SqlParameter("@GetAddress",obj.GetAddress),
                            new SqlParameter("@Text",obj.Text)
                                };
             return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }
        /// <summary>
        /// 删除
        /// </summary>
        public int Delete(Model.CNVP_Order obj)
        {
           StringBuilder sql = new StringBuilder();
           sql.Append("delete from " + Prefix + "Order where [ID] = @ID");
           SqlParameter[] pra = {
                                new SqlParameter("@ID",obj.ID)
						
                                };
           return DbHelper.ExecuteNonQuery(sql.ToString(),pra);
        }
        /// <summary>
        /// 查看单个
        /// </summary>
        public Model.CNVP_Order LoadById(int ID)
        {
            
            StringBuilder sql = new StringBuilder();
            sql.Append("select [OrderNum],[AddTime],[ProductsTotalAmount],[OrderTotalAmount],[DeliveryWay],[DeliveryPrice],[PayWay],[IsSupportValue],[SupportValuePrice],[ProductWeight],[PayFactorage],[BillRises],[IsNeedBill],[BilTaxes],[PayCurrency],[SalesPromotionAmount],[DiscountOrder],[MobilePhone],[Phone],[Email],[PostTime],[ZipCode],[GetTrueName], [Province], [City], [Area],[GetAddress],[BuyerID],[IsDelete],[OrderState],[Text],[DeliveryNo],[DeliveryCompany] from " + Prefix + "Order");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(),pra);
            Model.CNVP_Order cnvp_order = new Model.CNVP_Order();
            if (DT != null && DT.Rows.Count > 0)
            {
                DataRow dr = DT.Rows[0];
                cnvp_order.OrderNum = dr["OrderNum"] is DBNull ? String.Empty : (string)dr["OrderNum"];
                cnvp_order.AddTime = dr["AddTime"] is DBNull ? DateTime.Now : (DateTime)dr["AddTime"];
                cnvp_order.ProductsTotalAmount = dr["ProductsTotalAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["ProductsTotalAmount"];
                cnvp_order.OrderTotalAmount = dr["OrderTotalAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["OrderTotalAmount"];
                cnvp_order.DeliveryWay = dr["DeliveryWay"] is DBNull ? String.Empty : (string)dr["DeliveryWay"];
                cnvp_order.DeliveryPrice = dr["DeliveryPrice"] is DBNull ? (decimal)0.00 : (decimal)dr["DeliveryPrice"];
                cnvp_order.PayWay = dr["PayWay"] is DBNull ? String.Empty : (string)dr["PayWay"];
                cnvp_order.IsSupportValue = dr["IsSupportValue"] is DBNull ? (byte)0 : (byte)dr["IsSupportValue"];
                cnvp_order.SupportValuePrice = dr["SupportValuePrice"] is DBNull ? (decimal)0.00 : (decimal)dr["SupportValuePrice"];
                cnvp_order.ProductWeight = dr["ProductWeight"] is DBNull ? 0 : (int)dr["ProductWeight"];
                cnvp_order.PayFactorage = dr["PayFactorage"] is DBNull ? (decimal)0.00 : (decimal)dr["PayFactorage"];
                cnvp_order.BillRises = dr["BillRises"] is DBNull ? String.Empty : (string)dr["BillRises"];
                cnvp_order.IsNeedBill = dr["IsNeedBill"] is DBNull ? (byte)0 : (byte)dr["IsNeedBill"];
                cnvp_order.BilTaxes = dr["BilTaxes"] is DBNull ? (decimal)0.00 : (decimal)dr["BilTaxes"];
                cnvp_order.PayCurrency = dr["PayCurrency"] is DBNull ? String.Empty : (string)dr["PayCurrency"];
                cnvp_order.SalesPromotionAmount = dr["SalesPromotionAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["SalesPromotionAmount"];
                cnvp_order.DiscountOrder = dr["DiscountOrder"] is DBNull ? (decimal)0.00 : (decimal)dr["DiscountOrder"];
                cnvp_order.MobilePhone = dr["MobilePhone"] is DBNull ? String.Empty : (string)dr["MobilePhone"];
                cnvp_order.Phone = dr["Phone"] is DBNull ? String.Empty : (string)dr["Phone"];
                cnvp_order.Email = dr["Email"] is DBNull ? String.Empty : (string)dr["Email"];
                cnvp_order.PostTime = dr["PostTime"] is DBNull ? DateTime.Now : (DateTime)dr["PostTime"];
                cnvp_order.ZipCode = dr["ZipCode"] is DBNull ? String.Empty : (string)dr["ZipCode"];
                cnvp_order.GetTrueName = dr["GetTrueName"] is DBNull ? String.Empty : (string)dr["GetTrueName"];
                cnvp_order.Province = dr["Province"] is DBNull ? String.Empty : (string)dr["Province"];
                cnvp_order.City = dr["City"] is DBNull ? String.Empty : (string)dr["City"];
                cnvp_order.Area = dr["Area"] is DBNull ? String.Empty : (string)dr["Area"];
                cnvp_order.GetAddress = dr["GetAddress"] is DBNull ? String.Empty : (string)dr["GetAddress"];
                cnvp_order.BuyerID = dr["BuyerID"] is DBNull ? 0 : (int)dr["BuyerID"];
                cnvp_order.IsDelete = dr["IsDelete"] is DBNull ? (byte)0 : (byte)dr["IsDelete"];
                cnvp_order.OrderState = dr["OrderState"] is DBNull ? (byte)0 : (byte)dr["OrderState"];
                cnvp_order.Text = dr["Text"] is DBNull ? String.Empty : (string)dr["Text"];
                cnvp_order.DeliveryNo = dr["DeliveryNo"] is DBNull ? String.Empty : (string)dr["DeliveryNo"];
                cnvp_order.DeliveryCompany = dr["DeliveryCompany"] is DBNull ? String.Empty : (string)dr["DeliveryCompany"];
            }
            return cnvp_order;
        }
        /// <summary>
        /// 查看全部
        /// </summary>
        public List<Model.CNVP_Order> LoadAll(string sqlWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select [ID],[OrderNum],[AddTime],[ProductsTotalAmount],[OrderTotalAmount],[DeliveryWay],[DeliveryPrice],[PayWay],[IsSupportValue],[SupportValuePrice],[ProductWeight],[PayFactorage],[BillRises],[IsNeedBill],[BilTaxes],[PayCurrency],[SalesPromotionAmount],[DiscountOrder],[MobilePhone],[Phone],[Email],[PostTime],[ZipCode],[GetTrueName], [Province], [City], [Area],[GetAddress],[BuyerID],[IsDelete],[OrderState] from " + Prefix + "Order");
            if (sqlWhere.Trim() != "")
            {
                sql.Append(" where " + sqlWhere);
            }
            DataTable DT = DbHelper.ExecuteTable(sql.ToString());
            List<Model.CNVP_Order> list=new List<Model.CNVP_Order>();
            if (DT != null && DT.Rows.Count > 0)
            {
                foreach (DataRow dr in DT.Rows)
                {
                    Model.CNVP_Order cnvp_order = new Model.CNVP_Order();
                    cnvp_order.ID = dr["ID"] is DBNull ? 0 : (int)dr["ID"];
                    cnvp_order.OrderNum = dr["OrderNum"] is DBNull ? String.Empty : (string)dr["OrderNum"];
                    cnvp_order.AddTime = dr["AddTime"] is DBNull ? DateTime.Now : (DateTime)dr["AddTime"];
                    cnvp_order.ProductsTotalAmount = dr["ProductsTotalAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["ProductsTotalAmount"];
                    cnvp_order.OrderTotalAmount = dr["OrderTotalAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["OrderTotalAmount"];
                    cnvp_order.DeliveryWay = dr["DeliveryWay"] is DBNull ? String.Empty : (string)dr["DeliveryWay"];
                    cnvp_order.DeliveryPrice = dr["DeliveryPrice"] is DBNull ? (decimal)0.00 : (decimal)dr["DeliveryPrice"];
                    cnvp_order.PayWay = dr["PayWay"] is DBNull ? String.Empty : (string)dr["PayWay"];
                    cnvp_order.IsSupportValue = dr["IsSupportValue"] is DBNull ? (byte)0 : (byte)dr["IsSupportValue"];
                    cnvp_order.SupportValuePrice = dr["SupportValuePrice"] is DBNull ? (decimal)0.00 : (decimal)dr["SupportValuePrice"];
                    cnvp_order.ProductWeight = dr["ProductWeight"] is DBNull ? 0 : (int)dr["ProductWeight"];
                    cnvp_order.PayFactorage = dr["PayFactorage"] is DBNull ? (decimal)0.00 : (decimal)dr["PayFactorage"];
                    cnvp_order.BillRises = dr["BillRises"] is DBNull ? String.Empty : (string)dr["BillRises"];
                    cnvp_order.IsNeedBill = dr["IsNeedBill"] is DBNull ? (byte)0 : (byte)dr["IsNeedBill"];
                    cnvp_order.BilTaxes = dr["BilTaxes"] is DBNull ? (decimal)0.00 : (decimal)dr["BilTaxes"];
                    cnvp_order.PayCurrency = dr["PayCurrency"] is DBNull ? String.Empty : (string)dr["PayCurrency"];
                    cnvp_order.SalesPromotionAmount = dr["SalesPromotionAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["SalesPromotionAmount"];
                    cnvp_order.DiscountOrder = dr["DiscountOrder"] is DBNull ? (decimal)0.00 : (decimal)dr["DiscountOrder"];
                    cnvp_order.MobilePhone = dr["MobilePhone"] is DBNull ? String.Empty : (string)dr["MobilePhone"];
                    cnvp_order.Phone = dr["Phone"] is DBNull ? String.Empty : (string)dr["Phone"];
                    cnvp_order.Email = dr["Email"] is DBNull ? String.Empty : (string)dr["Email"];
                    cnvp_order.PostTime = dr["PostTime"] is DBNull ? DateTime.Now : (DateTime)dr["PostTime"];
                    cnvp_order.ZipCode = dr["ZipCode"] is DBNull ? String.Empty : (string)dr["ZipCode"];
                    cnvp_order.GetTrueName = dr["GetTrueName"] is DBNull ? String.Empty : (string)dr["GetTrueName"];
                    cnvp_order.Province = dr["Province"] is DBNull ? String.Empty : (string)dr["Province"];
                    cnvp_order.City = dr["City"] is DBNull ? String.Empty : (string)dr["City"];
                    cnvp_order.Area = dr["Area"] is DBNull ? String.Empty : (string)dr["Area"];
                    cnvp_order.GetAddress = dr["GetAddress"] is DBNull ? String.Empty : (string)dr["GetAddress"];
                    cnvp_order.BuyerID = dr["BuyerID"] is DBNull ? 0 : (int)dr["BuyerID"];
                    cnvp_order.IsDelete = dr["IsDelete"] is DBNull ? (byte)0 : (byte)dr["IsDelete"];
                    cnvp_order.OrderState = dr["OrderState"] is DBNull ? (byte)0 : (byte)dr["OrderState"];
                    list.Add(cnvp_order);
                }
            }
            return list;

        }
        /// <summary>
        /// 分页
        /// </summary>
        public DataTable GetPage(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string sql_Temp = "";
                string AllFields = "";
                string Condition = "";
                string IndexField = "";
                string OrderFields = "";
                if (SqlCondition[1].value.ToString() == "1")//后台管理中心使用语句
                {
                    if (SqlCondition[6].value.ToString() == "1")//高级搜索
                    {
                        if (SqlCondition[3].value.ToString() != "-1")
                        {
                            sql_Temp += " and  OrderState=" + SqlCondition[3].value.ToString() + "";
                        }
                        if (SqlCondition[2].value.ToString() != "")
                        {
                            sql_Temp += " and (OrderNum like '%" + SqlCondition[2].value.ToString() + "%' or [BuyerID] in (select id from " + Prefix + "Users where UserName like '%" + SqlCondition[2].value.ToString() + "%' ) or GetTrueName like '%" + SqlCondition[2].value.ToString() + "%'  or GetAddress like '%" + SqlCondition[2].value.ToString() + "%')  ";
                        }
                        if (SqlCondition[4].value.ToString() != "" && SqlCondition[5].value.ToString() != "")
                        {
                            sql_Temp += " and (CONVERT(varchar(100), AddTime, 23) >= '" + SqlCondition[4].value.ToString() + "' and CONVERT(varchar(100), AddTime, 23) <= '" + SqlCondition[5].value.ToString() + "')";
                        }
                        else if (SqlCondition[4].value.ToString() != "")
                        {
                            sql_Temp += " and '" + SqlCondition[4].value.ToString() + "' =CONVERT(varchar(10), AddTime, 120)";
                        }
                        else if (SqlCondition[5].value.ToString() != "")
                        {
                            sql_Temp += " and '" + SqlCondition[5].value.ToString() + "' =CONVERT(varchar(10), AddTime, 120)";
                        }
                    }
                    else
                    {
                        if (SqlCondition[0].value.ToString() != "-1")
                        {
                            sql_Temp += " and  OrderState=" + SqlCondition[0].value.ToString() + "";
                        }
                    }
                    
                    AllFields = "[ID],[OrderNum],[AddTime],[ProductsTotalAmount],[OrderTotalAmount],[DeliveryWay],[DeliveryPrice],[PayWay],[IsSupportValue],[SupportValuePrice],[ProductWeight],[PayFactorage],[BillRises],[IsNeedBill],[BilTaxes],[PayCurrency],[SalesPromotionAmount],[DiscountOrder],[MobilePhone],[Phone],[Email],[PostTime],[ZipCode],[GetTrueName], [Province], [City], [Area],[GetAddress],[BuyerID],[IsDelete],[OrderState]";
                    Condition = "" + Prefix + "Order where 1=1 " + sql_Temp + "";
                    IndexField = "ID";
                    OrderFields = "order by ID Desc";
                }
                else
                {
                    if (SqlCondition[0].value.ToString() != "-1")
                    {
                        if (SqlCondition[0].value.ToString() == "5,6,7")
                        {
                            sql_Temp += " and  OrderState in (" + SqlCondition[0].value.ToString() + ")";//注意In里不能为空
                        }
                        else
                        {
                            sql_Temp += " and  OrderState =" + SqlCondition[0].value.ToString() + "";
                        }
                        sql_Temp += " and [BuyerID]=" + SqlCondition[2].value.ToString() + "";
                        if (SqlCondition[3].value.ToString() != "")
                        {
                            sql_Temp += " and (OrderNum like '%" + SqlCondition[3].value.ToString() + "%' or [BuyerID] in (select id from " + Prefix + "Users where UserName like '%" + SqlCondition[3].value.ToString() + "%' ) or GetTrueName like '%" + SqlCondition[3].value.ToString() + "%'  or GetAddress like '%" + SqlCondition[3].value.ToString() + "%')  ";
                        }
                    }
                    else
                    {
                        sql_Temp += " and  [BuyerID]=" + SqlCondition[2].value.ToString() + "";
                        if (SqlCondition[3].value.ToString() != "")
                        {
                            sql_Temp += " and (OrderNum like '%" + SqlCondition[3].value.ToString() + "%' or [BuyerID] in (select id from " + Prefix + "Users where UserName like '%" + SqlCondition[3].value.ToString() + "%' ) or GetTrueName like '%" + SqlCondition[3].value.ToString() + "%'  or GetAddress like '%" + SqlCondition[3].value.ToString() + "%') ";
                        }
                    }

                    AllFields = "[ID],[OrderNum],[AddTime],[ProductsTotalAmount],[OrderTotalAmount],[DeliveryWay],[DeliveryPrice],[PayWay],[IsSupportValue],[SupportValuePrice],[ProductWeight],[PayFactorage],[BillRises],[IsNeedBill],[BilTaxes],[PayCurrency],[SalesPromotionAmount],[DiscountOrder],[MobilePhone],[Phone],[Email],[PostTime],[ZipCode],[GetTrueName], [Province], [City], [Area],[GetAddress],[IsDelete],[OrderState]";
                    Condition = "" + Prefix + "Order where 1=1 " + sql_Temp + "";
                    IndexField = "ID";
                    OrderFields = "order by ID Desc";
                }
               
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }
        #endregion

        /// <summary>
        /// 修改
        /// </summary>
        public int UpdateAddress(Model.CNVP_Order obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "Order set  [MobilePhone] = @MobilePhone,[Phone] = @Phone,[ZipCode] = @ZipCode,[GetTrueName] = @GetTrueName,[Province]=@Province,[City]=@City,[Area]=@Area,[GetAddress] = @GetAddress,[Text]=@Text where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@MobilePhone",obj.MobilePhone),
							new SqlParameter("@Phone",obj.Phone),
							new SqlParameter("@ZipCode",obj.ZipCode),
							new SqlParameter("@GetTrueName",obj.GetTrueName),
                            new SqlParameter("@Province",obj.Province),
							new SqlParameter("@City",obj.City),
							new SqlParameter("@Area",obj.Area),
							new SqlParameter("@GetAddress",obj.GetAddress),
                            new SqlParameter("@Text",obj.Text)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        /// <summary>
        /// 修改,准备发货(到付或已付)
        /// </summary>
        public int UpdateOrderShow3(Model.CNVP_Order obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "Order set [OrderTotalAmount] = @OrderTotalAmount,[DeliveryPrice] = @DeliveryPrice,[DiscountOrder] = @DiscountOrder,[OrderState] = @OrderState,[DeliveryNo]=@DeliveryNo,[DeliveryCompany]=@DeliveryCompany,[Text]=@Text where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@OrderTotalAmount",obj.OrderTotalAmount),
							new SqlParameter("@DeliveryPrice",obj.DeliveryPrice),
							new SqlParameter("@DiscountOrder",obj.DiscountOrder),
                            new SqlParameter("@OrderState",obj.OrderState),
                            new SqlParameter("@DeliveryNo",obj.DeliveryNo),
                            new SqlParameter("@DeliveryCompany",obj.DeliveryCompany),
                            new SqlParameter("@Text",obj.Text)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        /// <summary>
        /// 修改,等待付款
        /// </summary>
        public int UpdateOrderShow(Model.CNVP_Order obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "Order set [OrderTotalAmount] = @OrderTotalAmount,[DeliveryPrice] = @DeliveryPrice,[DiscountOrder] = @DiscountOrder,[OrderState] = @OrderState,[Text]=@Text where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
							new SqlParameter("@OrderTotalAmount",obj.OrderTotalAmount),
							new SqlParameter("@DeliveryPrice",obj.DeliveryPrice),
							new SqlParameter("@DiscountOrder",obj.DiscountOrder),
                            new SqlParameter("@OrderState",obj.OrderState),
                              new SqlParameter("@Text",obj.Text)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        /// <summary>
        /// 计算数量 by OrderState
        /// </summary>
        public int NumByBuyerIDAndOrderState(int BuyerID, string OrderStateArray)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select Count([ID]) from " + Prefix + "Order");
            sql.Append(" Where [BuyerID]=@BuyerID and [OrderState] in (" + OrderStateArray + ")");
            SqlParameter[] pra = {
                                 new SqlParameter("@BuyerID",BuyerID)
                                    };
            return Convert.ToInt32(DbHelper.ExecuteScalar(sql.ToString(), pra));

        }

        /// <summary>
        /// 修改,订单状态
        /// </summary>
        public int UpdateOrderState(Model.CNVP_Order obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update " + Prefix + "Order set [OrderState] = @OrderState where [ID] = @ID");

            SqlParameter[] pra = { 
                            new SqlParameter("@ID",obj.ID),
                            new SqlParameter("@OrderState",obj.OrderState)
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        /// <summary>
        /// 修改,订单状态By订单号
        /// </summary>
        public int UpdateOrderStateByNo(Model.CNVP_Order obj)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update [" + Prefix + "Order] set [OrderState] = @OrderState,[PayWay]=@PayWay where [OrderNum] = @OrderNum");

            SqlParameter[] pra = { 
                            new SqlParameter("@OrderNum",obj.OrderNum),
                            new SqlParameter("@OrderState",obj.OrderState),
                            new SqlParameter("@PayWay",obj.PayWay),
                                };
            return DbHelper.ExecuteNonQuery(sql.ToString(), pra);
        }

        /// <summary>
        /// 收货人信息,订单信息等
        /// </summary>
        public Model.CNVP_Order LoadDeliveryInfoById(int ID)
        {

            StringBuilder sql = new StringBuilder();
            sql.Append("select [OrderNum],[AddTime],[ProductsTotalAmount],[OrderTotalAmount],[DeliveryWay],[DeliveryPrice],[PayWay],[IsSupportValue],[SupportValuePrice],[ProductWeight],[PayFactorage],[BillRises],[IsNeedBill],[BilTaxes],[PayCurrency],[SalesPromotionAmount],[DiscountOrder],[MobilePhone],[Phone],[Email],[PostTime],[ZipCode],[GetTrueName], ");
            sql.Append(" (select isnull(sum(Num),0) from  CNVP_OrderProducts where OrderID=CNVP_Order.ID) as OrderProductsNum, ");
            sql.Append(" (select Name from " + Prefix + "Province where Code = [Province]) as Province_Name, (select Name from  " + Prefix + "City where Code= [City]) as City_Name , (select Name from " + Prefix + "Area where Code=[Area]) as Area_Name,[GetAddress],[BuyerID],[IsDelete],[OrderState],[Text],[DeliveryNo] from " + Prefix + "Order");
            sql.Append(" where [ID] = @ID");
            SqlParameter[] pra = {
                                 new SqlParameter("@ID",ID)
                                    };
            DataTable DT = DbHelper.ExecuteTable(sql.ToString(), pra);
            if (DT.Rows.Count > 0)
            {
                Model.CNVP_Order cnvp_order = new Model.CNVP_Order();
                DataRow dr = DT.Rows[0];
                cnvp_order.OrderNum = dr["OrderNum"] is DBNull ? String.Empty : (string)dr["OrderNum"];
                cnvp_order.AddTime = dr["AddTime"] is DBNull ? DateTime.Now : (DateTime)dr["AddTime"];
                cnvp_order.ProductsTotalAmount = dr["ProductsTotalAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["ProductsTotalAmount"];
                cnvp_order.OrderTotalAmount = dr["OrderTotalAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["OrderTotalAmount"];
                cnvp_order.DeliveryWay = dr["DeliveryWay"] is DBNull ? String.Empty : (string)dr["DeliveryWay"];
                cnvp_order.DeliveryPrice = dr["DeliveryPrice"] is DBNull ? (decimal)0.00 : (decimal)dr["DeliveryPrice"];
                cnvp_order.PayWay = dr["PayWay"] is DBNull ? String.Empty : (string)dr["PayWay"];
                cnvp_order.IsSupportValue = dr["IsSupportValue"] is DBNull ? (byte)0 : (byte)dr["IsSupportValue"];
                cnvp_order.SupportValuePrice = dr["SupportValuePrice"] is DBNull ? (decimal)0.00 : (decimal)dr["SupportValuePrice"];
                cnvp_order.ProductWeight = dr["ProductWeight"] is DBNull ? 0 : (int)dr["ProductWeight"];
                cnvp_order.PayFactorage = dr["PayFactorage"] is DBNull ? (decimal)0.00 : (decimal)dr["PayFactorage"];
                cnvp_order.BillRises = dr["BillRises"] is DBNull ? String.Empty : (string)dr["BillRises"];
                cnvp_order.IsNeedBill = dr["IsNeedBill"] is DBNull ? (byte)0 : (byte)dr["IsNeedBill"];
                cnvp_order.BilTaxes = dr["BilTaxes"] is DBNull ? (decimal)0.00 : (decimal)dr["BilTaxes"];
                cnvp_order.PayCurrency = dr["PayCurrency"] is DBNull ? String.Empty : (string)dr["PayCurrency"];
                cnvp_order.SalesPromotionAmount = dr["SalesPromotionAmount"] is DBNull ? (decimal)0.00 : (decimal)dr["SalesPromotionAmount"];
                cnvp_order.DiscountOrder = dr["DiscountOrder"] is DBNull ? (decimal)0.00 : (decimal)dr["DiscountOrder"];
                cnvp_order.MobilePhone = dr["MobilePhone"] is DBNull ? String.Empty : (string)dr["MobilePhone"];
                cnvp_order.Phone = dr["Phone"] is DBNull ? String.Empty : (string)dr["Phone"];
                cnvp_order.Email = dr["Email"] is DBNull ? String.Empty : (string)dr["Email"];
                cnvp_order.PostTime = dr["PostTime"] is DBNull ? DateTime.Now : (DateTime)dr["PostTime"];
                cnvp_order.ZipCode = dr["ZipCode"] is DBNull ? String.Empty : (string)dr["ZipCode"];
                cnvp_order.GetTrueName = dr["GetTrueName"] is DBNull ? String.Empty : (string)dr["GetTrueName"];
                cnvp_order.Province = dr["Province_Name"] is DBNull ? String.Empty : (string)dr["Province_Name"];
                cnvp_order.City = dr["City_Name"] is DBNull ? String.Empty : (string)dr["City_Name"];
                cnvp_order.Area = dr["Area_Name"] is DBNull ? String.Empty : (string)dr["Area_Name"];
                cnvp_order.GetAddress = dr["GetAddress"] is DBNull ? String.Empty : (string)dr["GetAddress"];
                cnvp_order.BuyerID = dr["BuyerID"] is DBNull ? 0 : (int)dr["BuyerID"];
                cnvp_order.IsDelete = dr["IsDelete"] is DBNull ? (byte)0 : (byte)dr["IsDelete"];
                cnvp_order.OrderState = dr["OrderState"] is DBNull ? (byte)0 : (byte)dr["OrderState"];
                cnvp_order.Text = dr["Text"] is DBNull ? String.Empty : (string)dr["Text"];
                cnvp_order.DeliveryNo = dr["DeliveryNo"] is DBNull ? String.Empty : (string)dr["DeliveryNo"];
                cnvp_order.OrderProductsNum = dr["OrderProductsNum"] is DBNull ? 0 : (int)dr["OrderProductsNum"];//自定义
                cnvp_order.WebShopName = Config.UIConfig.SiteName;//自定义
                return cnvp_order;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        public DataTable GetPageReview(string PageName, int PageIndex, int PageSize, out int RecordCount, out int PageCount, params SQLConditionInfo[] SqlCondition)
        {
            try
            {
                string sql_Temp = "";
                sql_Temp += " and a.[BuyerID]=" + SqlCondition[1].value.ToString() + "";
                if (SqlCondition[2].value.ToString() != "0")
                {
                    sql_Temp += " and b.[ProductID] =" + SqlCondition[2].value.ToString() + "";
                }
                string AllFields = "a.[ID],a.[OrderNum],a.[PostTime]";
                string Condition = "" + Prefix + "Order as a left join " + Prefix + "OrderProducts as b on a.[ID]=b.[OrderID] left join " + Prefix + "Products as c on c.[ID] = b.[ProductID] where c.[AllowReview] = 1 and a.OrderState in (" + SqlCondition[0].value.ToString() + ")" + sql_Temp + "";
                string IndexField = "a.ID";
                string OrderFields = " group by a.[ID], a.[OrderNum],a.[PostTime] order by a.ID Desc";
                return DbHelper.ExecutePage(AllFields, Condition, IndexField, OrderFields, PageIndex, PageSize, out RecordCount, out PageCount, null);
            }
            catch (Exception e)
            {
                throw new Exception("用于分页的SQL语句无效:" + e.Message);
            }

        }
    }
}

